package com.easyadmin.controller;

import com.easyadmin.entity.constants.Constants;
import com.easyadmin.entity.dto.SessionUserDto;
import com.easyadmin.entity.enums.ResponseCodeEnum;
import com.easyadmin.entity.enums.ResponseStatusEnum;
import com.easyadmin.entity.vo.AjaxResponseVO;
import com.easyadmin.exception.BusinessException;

import javax.servlet.http.HttpSession;

public class ABaseController {

    protected <T> AjaxResponseVO getServerErrorResponseVO() {
        AjaxResponseVO vo = new AjaxResponseVO();
        vo.setCode(ResponseCodeEnum.CODE_500.getCode());
        vo.setInfo(ResponseCodeEnum.CODE_500.getDesc());
        vo.setStatus(ResponseStatusEnum.ERROR.getStatus());
        return vo;
    }


    protected <T> AjaxResponseVO getSuccessResponseVO(T t) {
        AjaxResponseVO<T> responseVO = new AjaxResponseVO<>();
        responseVO.setCode(ResponseCodeEnum.CODE_200.getCode());
        responseVO.setInfo(ResponseCodeEnum.CODE_200.getDesc());
        responseVO.setData(t);
        return responseVO;
    }

    protected <T> AjaxResponseVO getBusinessErrorResponseVO(BusinessException e) {
        AjaxResponseVO vo = new AjaxResponseVO();
        if (e.getCode() == null) {
            vo.setCode(ResponseCodeEnum.CODE_600.getCode());
        } else {
            vo.setCode(e.getCode());
        }
        vo.setInfo(e.getMessage());
        vo.setStatus(ResponseStatusEnum.ERROR.getStatus());
        return vo;
    }

    protected SessionUserDto getSessionUserInfo(HttpSession session) {
        SessionUserDto sessionUserDto = (SessionUserDto) session.getAttribute(Constants.ADMIN_SESSION_KEY);
        return sessionUserDto;
    }
}
